﻿/*
* accordion - jQuery plugin 1.0.0
*
* Copyright (c) 2010 liugq
*
*
* revision: $Id: jquery.accordion.js   2010-07-06 by liugq
*
*/
(function(jQuery) {
    jQuery.fn.accordion = function(options) {
        var options = jQuery.extend({}, jQuery.fn.accordion.defaults, options);
        if (options.displayonlyone && options.active == -1) {
            options.active = 0;
        }
        options.toppachage = options.accordion_item_1 + options.accordion_item_2 + options.accordion_item_3;
        options.bottompachage = options.accordion_item_4 + options.accordion_item_5 + options.accordion_item_1;

        return this.each(function() {
            jQuery.Accordion(this, options);
        });
    };


    jQuery.Accordion = function(c, options) {
        createPackage();
        initContentHeight();
        menuItemEvent();
        showAll();
        bindHeaderEvent();

        function createPackage() {
            jQuery(".accordion-item", c).prepend(options.toppachage).append(options.bottompachage);
        }
        function menuItemEvent() {
            jQuery(".accordion-menu-item", c).mousemove(function() {
                jQuery(this).addClass("accordion-menu-item-over");
            }).mouseout(function() {
                jQuery(this).removeClass("accordion-menu-item-over");
            });
        }
        function initContentHeight() {
            if (options.displayonlyone) {
                var size = jQuery(c).children(".accordion-item").size();
                var accordion_content_height = jQuery(c).height() - size * 25 - 2;
                jQuery(c).children(".accordion-item").children(".accordion-content").height(accordion_content_height);
            }
        }
        function showAll() {
            if (options.expandall && !options.displayonlyone)
                jQuery(".accordion-content", c).slideDown(0);
        }
        function bindHeaderEvent() {
            jQuery(".accordion-header", c).each(function(i) {
                var content = jQuery(this).next(".accordion-content");
                showOne(content, i);
                headerEvent(this, content);
            });
        }
        function showOne(content, i) {
            if (i == options.active && (!options.expandall || options.displayonlyone))
                jQuery(content).slideDown(0, function() {
                    if (options.displayonlyone) {
                        options.currentdisplay = content;
                    }
                });
        }
        function headerEvent(header, content) {
            var child = isChildItem(jQuery(header).parent());
            jQuery(header).bind(options.event, function() {
                var dis = jQuery(content).css('display');
                if (dis == "none") {
                    if (options.displayonlyone && !child) {
                        jQuery(options.currentdisplay).slideUp(0);
                        options.currentdisplay = content;
                    }
                    jQuery(content).slideDown(0);
                }
                else {
                    if (!options.displayonlyone || child)
                        jQuery(content).slideUp(0);
                }
            });
        }
        function isChildItem(item) {
            var size = jQuery(item).parent(".accordion-content").size();
            if (size > 0)
                return true;
            else
                return false;
        }
    };

    jQuery.fn.accordion.defaults = {
        accordion_item_1: '<div class="accordion-item_1"/>',
        accordion_item_2: '<div class="accordion-item_2"/>',
        accordion_item_3: '<div class="accordion-item_3"/>',
        accordion_item_4: '<div class="accordion-item_4"/>',
        accordion_item_5: '<div class="accordion-item_5"/>',
        height: 500,
        autoheight: true,
        event: "click",
        expandall: false,
        displayonlyone: false,
        active: -1
    };
})(jQuery)